<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>我的申请 - 售后管理系统</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Microsoft YaHei', Arial, sans-serif;
            background-color: #f5f5f5;
        }

        .header {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            padding: 20px;
            display: flex;
            justify-content: space-between;
            align-items: center;
        }

        .header h1 {
            font-size: 24px;
        }

        .back-btn {
            background: rgba(255, 255, 255, 0.2);
            border: 1px solid rgba(255, 255, 255, 0.3);
            color: white;
            padding: 8px 16px;
            border-radius: 5px;
            cursor: pointer;
            text-decoration: none;
        }

        .container {
            max-width: 1200px;
            margin: 0 auto;
            padding: 20px;
        }

        .filter-section {
            background: white;
            border-radius: 10px;
            padding: 20px;
            margin-bottom: 20px;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
        }

        .filter-row {
            display: flex;
            gap: 20px;
            align-items: center;
            flex-wrap: wrap;
        }

        .filter-group {
            display: flex;
            align-items: center;
            gap: 10px;
        }

        .filter-group label {
            font-weight: 500;
            color: #333;
        }

        .filter-group select {
            padding: 8px 12px;
            border: 2px solid #e1e5e9;
            border-radius: 5px;
            font-size: 14px;
        }

        .filter-btn {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            border: none;
            padding: 8px 16px;
            border-radius: 5px;
            cursor: pointer;
            font-size: 14px;
        }

        .application-list {
            background: white;
            border-radius: 10px;
            padding: 20px;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
        }

        .application-item {
            border: 1px solid #e1e5e9;
            border-radius: 8px;
            padding: 20px;
            margin-bottom: 15px;
            transition: box-shadow 0.2s ease;
        }

        .application-item:hover {
            box-shadow: 0 4px 15px rgba(0, 0, 0, 0.1);
        }

        .application-header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 15px;
        }

        .application-id {
            font-size: 18px;
            font-weight: bold;
            color: #333;
        }

        .status-badge {
            padding: 4px 12px;
            border-radius: 20px;
            font-size: 12px;
            font-weight: 500;
        }

        .status-pending {
            background-color: #fff3cd;
            color: #856404;
        }

        .status-processing {
            background-color: #cce5ff;
            color: #004085;
        }

        .status-completed {
            background-color: #d4edda;
            color: #155724;
        }

        .status-rejected {
            background-color: #f8d7da;
            color: #721c24;
        }

        .application-details {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
            gap: 15px;
            margin-bottom: 15px;
        }

        .detail-item {
            display: flex;
            flex-direction: column;
        }

        .detail-label {
            font-size: 12px;
            color: #666;
            margin-bottom: 5px;
        }

        .detail-value {
            font-size: 14px;
            color: #333;
            font-weight: 500;
        }

        .application-actions {
            display: flex;
            gap: 10px;
            flex-wrap: wrap;
        }

        .action-btn {
            padding: 6px 12px;
            border: 1px solid #667eea;
            background: white;
            color: #667eea;
            border-radius: 5px;
            cursor: pointer;
            font-size: 12px;
            transition: all 0.2s ease;
        }

        .action-btn:hover {
            background: #667eea;
            color: white;
        }

        .action-btn.primary {
            background: #667eea;
            color: white;
        }

        .action-btn.primary:hover {
            background: #5a6fd8;
        }

        .empty-state {
            text-align: center;
            padding: 40px;
            color: #666;
        }

        .empty-state h3 {
            margin-bottom: 10px;
            color: #333;
        }

        .loading {
            text-align: center;
            padding: 40px;
        }

        .spinner {
            border: 2px solid #f3f3f3;
            border-top: 2px solid #667eea;
            border-radius: 50%;
            width: 30px;
            height: 30px;
            animation: spin 1s linear infinite;
            display: inline-block;
        }

        @keyframes spin {
            0% { transform: rotate(0deg); }
            100% { transform: rotate(360deg); }
        }

        .pagination {
            display: flex;
            justify-content: center;
            gap: 10px;
            margin-top: 20px;
        }

        .page-btn {
            padding: 8px 12px;
            border: 1px solid #e1e5e9;
            background: white;
            color: #333;
            border-radius: 5px;
            cursor: pointer;
            transition: all 0.2s ease;
        }

        .page-btn:hover {
            background: #f8f9fa;
        }

        .page-btn.active {
            background: #667eea;
            color: white;
            border-color: #667eea;
        }
    </style>
</head>
<body>
    <div class="header">
        <h1>我的申请</h1>
        <a href="/after_sale_system/page/consumer" class="back-btn">返回首页</a>
    </div>

    <div class="container">
        <div class="filter-section">
            <div class="filter-row">
                <div class="filter-group">
                    <label>状态筛选:</label>
                    <select id="statusFilter">
                        <option value="">全部状态</option>
                        <option value="PENDING_AUDIT">待审核</option>
                        <option value="PROCESSING">处理中</option>
                        <option value="COMPLETED">已完成</option>
                        <option value="REJECTED">已拒绝</option>
                    </select>
                </div>
                <div class="filter-group">
                    <label>类型筛选:</label>
                    <select id="typeFilter">
                        <option value="">全部类型</option>
                        <option value="RETURN">退货</option>
                        <option value="EXCHANGE">换货</option>
                        <option value="REPAIR">维修</option>
                    </select>
                </div>
                <button class="filter-btn" onclick="loadApplications()">筛选</button>
            </div>
        </div>

        <div class="application-list">
            <div id="applicationContainer">
                <div class="loading">
                    <div class="spinner"></div>
                    <p>加载中...</p>
                </div>
            </div>
        </div>
    </div>

    <script>
        let currentPage = 1;
        let totalPages = 1;

        // 页面加载时获取申请列表
        window.addEventListener('load', function() {
            loadApplications();
        });

        // 加载申请列表
        function loadApplications() {
            const statusFilter = document.getElementById('statusFilter').value;
            const typeFilter = document.getElementById('typeFilter').value;
            
            const params = new URLSearchParams();
            if (statusFilter) params.append('status', statusFilter);
            if (typeFilter) params.append('type', typeFilter);
            params.append('page', currentPage);

            fetch(`/after_sale_system/api/afterSale/?${params.toString()}`)
                .then(response => response.json())
                .then(data => {
                    if (data.success) {
                        displayApplications(data.data);
                        totalPages = data.totalPages || 1;
                        updatePagination();
                    } else {
                        showError('获取申请列表失败: ' + data.message);
                    }
                })
                .catch(error => {
                    console.error('获取申请列表失败:', error);
                    showError('获取申请列表失败，请稍后重试');
                });
        }

        // 显示申请列表
        function displayApplications(applications) {
            const container = document.getElementById('applicationContainer');
            
            if (!applications || applications.length === 0) {
                container.innerHTML = `
                    <div class="empty-state">
                        <h3>暂无申请记录</h3>
                        <p>您还没有提交过售后申请</p>
                    </div>
                `;
                return;
            }

            container.innerHTML = applications.map(app => `
                <div class="application-item">
                    <div class="application-header">
                        <div class="application-id">申请编号: ${app.id}</div>
                        <div class="status-badge status-${getStatusClass(app.status)}">${getStatusText(app.status)}</div>
                    </div>
                    
                    <div class="application-details">
                        <div class="detail-item">
                            <div class="detail-label">申请类型</div>
                            <div class="detail-value">${getTypeText(app.afterSaleType)}</div>
                        </div>
                        <div class="detail-item">
                            <div class="detail-label">申请时间</div>
                            <div class="detail-value">${formatDate(app.applyTime)}</div>
                        </div>
                        <div class="detail-item">
                            <div class="detail-label">订单号</div>
                            <div class="detail-value">${app.orderId || 'N/A'}</div>
                        </div>
                        <div class="detail-item">
                            <div class="detail-label">商品名称</div>
                            <div class="detail-value">${app.productName || 'N/A'}</div>
                        </div>
                    </div>
                    
                    <div class="application-actions">
                        <button class="action-btn" onclick="viewDetails(${app.id})">查看详情</button>
                        <button class="action-btn" onclick="viewCommunication(${app.id})">沟通记录</button>
                        ${app.status === 'PENDING_AUDIT' ?`
                            <button class="action-btn" onClick="editApplication(${app.id})">修改申请</button>
                            <button class="action-btn" onclick="cancelApplication(${app.id})">取消申请</button>` : ''
                        }
                        ${app.status === 'COMPLETED' ? 
                            `<button class="action-btn primary" onclick="submitFeedback(${app.id})">提交反馈</button>` : ''
                        }
                    </div>
                </div>
            `).join('');
        }

        // 获取状态样式类
        function getStatusClass(status) {
            const statusMap = {
                'PENDING_AUDIT': 'pending',
                'PROCESSING': 'processing',
                'COMPLETED': 'completed',
                'REJECTED': 'rejected'
            };
            return statusMap[status] || 'pending';
        }

        // 获取状态文本
        function getStatusText(status) {
            const statusMap = {
                'PENDING_AUDIT': '待审核',
                'PROCESSING': '处理中',
                'COMPLETED': '已完成',
                'REJECTED': '已拒绝'
            };
            return statusMap[status] || status;
        }

        // 获取类型文本
        function getTypeText(type) {
            const typeMap = {
                'RETURN': '退货',
                'EXCHANGE': '换货',
                'REPAIR': '维修'
            };
            return typeMap[type] || type;
        }

        // 格式化日期
        function formatDate(dateString) {
            if (!dateString) return 'N/A';
            const date = new Date(dateString);
            return date.toLocaleString('zh-CN');
        }

        // 查看详情
        function viewDetails(id) {
            window.location.href = `/after_sale_system/page/consumer/applicationDetail?id=${id}`;
        }

        // 查看沟通记录
        function viewCommunication(id) {
            window.location.href = `/after_sale_system/page/consumer/communication?id=${id}`;
        }

        //修改申请
        function editApplication(id) {
            window.location.href = `/after_sale_system/page/consumer/applicationEdit?id=${id}`;
        }

        // 取消申请
        function cancelApplication(id) {
            if (confirm('确定要取消这个申请吗？')) {
                fetch(`/after_sale_system/api/afterSale/cancel/${id}`, {
                    method: 'DELETE'
                })
                .then(response => response.json())
                .then(data => {
                    if (data.success) {
                        alert('申请已取消');
                        loadApplications();
                    } else {
                        alert('取消失败: ' + data.message);
                    }
                })
                .catch(error => {
                    console.error('取消申请失败:', error);
                    alert('取消申请失败，请稍后重试');
                });
            }
        }

        // 提交反馈
        function submitFeedback(id) {
            window.location.href = `/after_sale_system/page/consumer/feedback?id=${id}`;
        }

        // 更新分页
        function updatePagination() {
            const pagination = document.createElement('div');
            pagination.className = 'pagination';
            
            // 上一页
            if (currentPage > 1) {
                pagination.innerHTML += `<button class="page-btn" onclick="changePage(${currentPage - 1})">上一页</button>`;
            }
            
            // 页码
            for (let i = 1; i <= totalPages; i++) {
                if (i === currentPage) {
                    pagination.innerHTML += `<button class="page-btn active">${i}</button>`;
                } else {
                    pagination.innerHTML += `<button class="page-btn" onclick="changePage(${i})">${i}</button>`;
                }
            }
            
            // 下一页
            if (currentPage < totalPages) {
                pagination.innerHTML += `<button class="page-btn" onclick="changePage(${currentPage + 1})">下一页</button>`;
            }
            
            const container = document.getElementById('applicationContainer');
            container.appendChild(pagination);
        }

        // 切换页面
        function changePage(page) {
            currentPage = page;
            loadApplications();
        }

        function showError(message) {
            alert(message);
        }
    </script>
</body>
</html> 